<?php
header('Content-Type:text/html;charset=utf8');

//visits_details  企业访问详情
include_once './db.php';
include_once './function.php';
$db = DB::getIntance();
$db_table = 'days_visits';

$cpid = (int)$_GET['cpid'];
$date = $_GET['date'];

$bool = valid_date($date);
if ($bool == false) {
    echo json_encode(array('code' => 0, 'msg' => '日期错误', 'data' => '',));
    exit();
}

$res1 = $db->getRow("select visit from " . $db_table . " where cpid = '" . $cpid . "' and `date` = '" . $date . "'");
if (empty($res1)) {
    echo json_encode(array('code' => 0, 'msg' => '此日期无访问记录', 'data' => '',));
    exit();
}
$data['cur_visit'] = $res1['visit'];

$res2 = $db->getRow("select sum(visit) as all_visit from " . $db_table . " where cpid = '" . $cpid . "'");
if (empty($res2)) {
    echo json_encode(array('code' => 0, 'msg' => '此企业无访问记录', 'data' => '',));
    exit();
}
$data['all_visit'] = $res2['all_visit'];

$start_date = date('Y-m-d', (strtotime($date) - 86400 * 30));
$date_arr = getDateFromRange($start_date, $date);

foreach ($date_arr as $value) {
    $res3 = $db->getRow("select visit from " . $db_table . " where cpid = '" . $cpid . "' and `date` = '" . $value . "'");
    $data['res'][$value] = $res3['visit'] ? $res3['visit'] : 0;
}

echo json_encode(array('code' => 1, 'msg' => 'succ', 'data' => $data));
exit();